﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;


namespace Oplaco.Exceptions
{
    /// <summary>
    /// Handler class for entlib exceptions
    /// </summary>
    public static class AppService
    {
        #region Process exceptions
        
        /// <summary>
        /// Demonstrates handling of exceptions coming out of a layer. The policy
        /// demonstrated here will show how original exceptions can be propagated back out.
        /// </summary>
        public static bool ProcessAndNotifyPresentationLayerException(Exception exception)
        {
            try
            {
                throw new PresentationLayerException(exception.Message, exception);
            }
            catch (Exception ex)
            {
                // Quick Start is configured so that the Propagate Policy will
                // log the exception and then recommend a rethrow.
                bool rethrow = ExceptionPolicy.HandleException(ex, "Notify Policy");

                if (rethrow)
                {
                    throw;
                }
            }

            return true;
        }

        /// <summary>
        /// Demonstrates handling of exceptions coming out of a layer. The policy
        /// demonstrated here will show how original exceptions can be propagated back out.
        /// </summary>
        public static bool ProcessAndNotifyBusinessLayerException(Exception exception)
        {
            try
            {
                throw new BusinessLayerException(exception.Message, exception);
            }
            catch (Exception ex)
            {
                // Quick Start is configured so that the Propagate Policy will
                // log the exception and then recommend a rethrow.
                bool rethrow = ExceptionPolicy.HandleException(ex, "Notify Policy");

                if (rethrow)
                {
                    throw;
                }
            }

            return true;
        }
        

        /// <summary>
        /// Demonstrates handling of exceptions coming out of a layer. The policy
        /// demonstrated here will show how original exceptions can be propagated back out.
        /// </summary>
        public static bool ProcessAndNotifyDataAccessLayerException(Exception exception)
        {
            try
            {
                throw new DataAccessLayerException(exception.Message, exception);
            }
            catch (Exception ex)
            {
                // Quick Start is configured so that the Propagate Policy will
                // log the exception and then recommend a rethrow.
                bool rethrow = ExceptionPolicy.HandleException(ex, "Notify Policy");

                if (rethrow)
                {
                    throw;
                }
            }

            return true;
        }

        /// <summary>
        /// Process any unhandled exceptions that occur in the application.
        /// This code is called by all UI entry points in the application (e.g. button click events)
        /// when an unhandled exception occurs.
        /// You could also achieve this by handling the Application.ThreadException event, however
        /// the VS2005 debugger will break before this event is called.
        /// </summary>
        /// <param name="exception">The unhandled exception</param>
        public static String ProcessUnhandledException(Exception exception)
        {
            string errorMsg = "An unexpected exception occured while calling HandleException with policy 'Global Policy'. ";
            // An unhandled exception occured somewhere in our application. Let
            // the 'Global Policy' handler have a try at handling it.
            try
            {
                bool rethrow = ExceptionPolicy.HandleException(exception, "Global Policy");
                if (rethrow)
                {
                    // Something has gone very wrong - exit the application.

                }
            }
            catch
            {
                // Something has gone wrong during HandleException (e.g. incorrect configuration of the block).
                // Exit the application
                throw exception;

            }
            return errorMsg;
        }
        #endregion

        #region Replace exceptions
        /// <summary>
        /// Demonstrates handling of exceptions coming out of a layer. The policy
        /// demonstrated here will show how original exceptions can be propagated back out.
        /// </summary>
        public static void ReplaceWithBusinessLayerException(Exception exception)
        {
            bool rethrow = false;
            rethrow = ExceptionPolicy.HandleException(exception, "Replace Policy");
            if (rethrow)
            {
                throw exception;
            }
        }

        /// <summary>
        /// Replaces generic exception with type DataAccessLayer exception 
        /// </summary>
        public static void ReplaceWithDataAccessLayerException(Exception exception)
        {
            bool rethrow = false;
            try
            {
                throw new DataAccessLayerException(exception.Message, exception);
            }
            catch (Exception ex) {
                rethrow = ExceptionPolicy.HandleException(ex, "Replace Policy");
                if (rethrow)
                {
                    throw;
                }
            }
            
        }

        /// <summary>
        /// Demonstrates handling of exceptions coming out of a layer. The policy
        /// demonstrated here will show how original exceptions can be propagated back out.
        /// </summary>
        public static bool ReplaceWithPresentationLayerException(Exception exception)
        {
            try
            {
                throw new PresentationLayerException(exception.Message, exception);
            }
            catch (Exception ex)
            {
                // Quick Start is configured so that the Propagate Policy will
                // log the exception and then recommend a rethrow.
                bool rethrow = ExceptionPolicy.HandleException(ex, "Replace Policy");

                if (rethrow)
                {
                    throw;
                }
            }

            return true;
        }

        #endregion


        /// <summary>
        /// Logger method
        /// </summary>
        /// <param name="exception">The exception to be logged</param>
        /// <returns></returns>
        public static bool LogException(Exception exception) {
            bool rethrow = false;
            try
            {
                // Quick Start is configured so that the Propagate Policy will
                // log the exception and then recommend a rethrow.
               rethrow = ExceptionPolicy.HandleException(exception, "Log Only Policy");
            }
            catch (Exception ex)
            {             

                if (rethrow)
                {
                    throw ex;
                }
            }

            return rethrow;
        }
    }
}
